package com.hue.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;

@RestController
public class FlowLimitController {
    @GetMapping("/testA")
    public String testA() throws InterruptedException {
//        TimeUnit.SECONDS.sleep(1);
        return "testA";
    }

    @GetMapping("/testB")
    public String testB(){
        System.out.println(Thread.currentThread().getName()+":testB");
        return "testB";
    }

    @GetMapping("/testC")
    public String testC() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        return "testC";
    }

    @GetMapping("/testD")
    public String testD() throws InterruptedException {
        int i=10/0;
        return "testD";
    }

    @GetMapping("/testE")
    public String testE() throws InterruptedException {
        int i=10/0;
        return "testE";
    }

    //测试热点限流
    @GetMapping("/hotKey")
    @SentinelResource(value = "hotKey",blockHandler = "deal_hotKey")
    public String hotKey(@RequestParam("a") String a){
        //sentinel的blockHandler不会管运行时的异常，只管请求是否违背的配置的规则
        int i=10/0;
        return "hotKey正常执行";
    }
    //限流之后调用的方法
    public String deal_hotKey(@RequestParam("a") String a, BlockException exception){
        return "热点被限流";
    }
}
